package com.ewing.busi.carry.dao;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.ewing.busi.carry.constants.RegionType;
import com.ewing.busi.carry.model.WebCarryWay;
import com.ewing.core.jdbc.BaseDao;

/**
 * 
 * @author chenxuegui@163.com
 * @createDate 2016年9月3日
 */
@Repository("webCarryWayDao")
public class WebCarryWayDao {

  @Resource
  private BaseDao baseDao;

  /**
   * 
   * @return
   * @author Joeson
   */
  public List<WebCarryWay> findByTemplateIdAndRegion(Integer webCarrayTemplateId, String region) {
    Class<WebCarryWay> clazz = WebCarryWay.class;
    String sql = " FROM " + clazz.getName() + " WHERE carry_template_id = " + webCarrayTemplateId
        + " AND ((region_name like '%" + region + "%' AND region_type=" + RegionType.SPECIFY.value
        + ") OR (region_type = " + RegionType.GLOBAL.value + "))";

    return baseDao.queryBySql(sql, WebCarryWay.class);
  }

  /**
   * 
   * @return
   * @author Joeson
   */
  public List<WebCarryWay> findByTemplateIdsAndRegion(List<Integer> arrayList, String region) {
    if (CollectionUtils.isEmpty(arrayList) || StringUtils.isEmpty(region)) {
      return Collections.emptyList();
    }

    Class<WebCarryWay> clazz = WebCarryWay.class;
    String sql = " FROM " + clazz.getName() + " WHERE carry_template_id IN ("
        + StringUtils.join(arrayList.toArray(new Integer[] {}), ",") + ") AND ((region_name like '%"
        + region + "%' AND region_type=" + RegionType.SPECIFY.value + ") OR (region_type = "
        + RegionType.GLOBAL.value + "))";

    return baseDao.queryBySql(sql, WebCarryWay.class);
  }

  public List<WebCarryWay> findByIds(List<Integer> ids) {
    if (CollectionUtils.isEmpty(ids)) {
      return Collections.emptyList();
    }

    return baseDao.findMuti(ids, WebCarryWay.class);
  }

}
